package com.mls.guardian.executor.slave;

import com.mls.guardian.common.RemoteUtil;
import com.mls.guardian.container.SlaveContainer;
import com.mls.guardian.dataobj.ServerProcessInfo;
import com.mls.guardian.protocol.RemoteBody;
import com.mls.guardian.protocol.RemoteCommand;
import com.mls.guardian.protocol.body.AppRestartBody;
import com.mls.guardian.remoting.executor.RemoteExecutor;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by root on 16-1-7.
 */
public class SlaveStartAppExecutor implements RemoteExecutor {
    private static final Logger log = LoggerFactory.getLogger(SlaveRestartAppExecutor.class);
    private SlaveContainer container;

    public SlaveStartAppExecutor(SlaveContainer container) {
        this.container = container;
    }

    @Override
    public RemoteBody execute(ChannelHandlerContext ctx, RemoteCommand request) throws Exception {
        AppRestartBody appRestart = (AppRestartBody) request.getRemoteBody();


        log.info("slave 收到启动应用请求 请求id是{}", request.getRequestId());
        List<ServerProcessInfo> list = appRestart.getServerProcessInfoListList();

        for (ServerProcessInfo serverInfo : list) {
            if (serverInfo.getIp().equals(RemoteUtil.getLocalHostIP())) {
                return container.getAppController().startApp(appRestart.getProjectName(), serverInfo.getProcessList());
            }
        }

        appRestart.setSuc(true);
        return appRestart;
    }
}
